package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodsServiceImpl implements GoodsService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Resource
    private TbGoodsMapper tbGoodsMapper;

    @Resource
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Resource
    private TbItemMapper tbItemMapper;

    @Resource
    private TbItemCatMapper itemCatMapper;

    @Transactional
    @Override
    public int insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,List<TbItem> tbItems,String sellerName,Long parentId) {
        tbGoodsMapper.insertGoods(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        for (TbItem tbItem : tbItems){
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            tbItem.setGoodsId(tbGoods.getId());
            tbItem.setSellerId(tbGoods.getSellerId());
            tbItem.setSeller(sellerName);
            tbItem.setCategoryid(parentId);
            tbItem.setStatus("1");
            tbItemMapper.insertSelective(tbItem);
        }
        logger.info("执行成功");
        return 1;
    }

    @Override
    public PageInfo<TbGoods> getListGoods(String goodsName, String auditStatus,Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<TbGoods> tbGoods = tbGoodsMapper.getListGoods(goodsName,auditStatus);
        List<TbGoods> tbGoodsList = new ArrayList<>();
        for (TbGoods tbGoods1 : tbGoods) {
            TbItemCat itemCat1 = itemCatMapper.selectByPrimaryKey(tbGoods1.getCategory1Id());
            TbItemCat itemCat2 = itemCatMapper.selectByPrimaryKey(tbGoods1.getCategory2Id());
            TbItemCat itemCat3 = itemCatMapper.selectByPrimaryKey(tbGoods1.getCategory3Id());
            tbGoods1.setCategory1Name(itemCat1.getName());
            tbGoods1.setCategory2Name(itemCat2.getName());
            tbGoods1.setCategory3Name(itemCat3.getName());
            tbGoodsList.add(tbGoods1);
        }
        PageInfo<TbGoods> pageInfo = new PageInfo<>(tbGoods);
        logger.info("执行成功");
        return pageInfo;
    }

    @Override
    public int updateGoodsAuditStatus(Long[] ids,String auditStatus) {
        return tbGoodsMapper.updateGoodsAuditStatus(ids,auditStatus);
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoodsDesc getGoodsDescById(Long goodsId) {
        return tbGoodsDescMapper.selectByPrimaryKey(goodsId);
    }
}
